home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk192 / unzipv3.1 / unzip.h < prev    next >
C/C++ Source or Header  |  1995-03-19  |  11KB  |  415 lines

  1. /*
  2.  * unzip.c was getting TOO big, so I split out a bunch of likely
  3.  * defines and constants into a separate file.
  4.  * David Kirschbaum
  5.  * Toad Hall
  6.  */
  7.  
  8. #define VERSION  "UnZip:  Zipfile Extract v3.10 (C) of 08-16-90;  (C) 1989 Samuel H. Smith"
  9.  
  10. /* #define NOSKIP 1 */    /* v3.04 Enable if you do NOT want
  11.              * skip_to_signature() enabled */
  12.  
  13. typedef unsigned char byte; /* code assumes UNSIGNED bytes */
  14. typedef long longint;
  15. typedef unsigned short UWORD;
  16. typedef char boolean;
  17.  
  18. /* v2.0g Allan Bjorklund added this.  Confirm it doesn't make your system
  19.  * choke and die!  (Could be it belongs down in the !__STDC__ section
  20.  * along with *malloc()
  21.  */
  22. #ifdef MTS        /* v2.0h No one else seems to want it tho... */
  23. #include <sys/file.h>   /* v2.0g Chitra says MTS needs this for O_BINARY */
  24. #endif
  25.  
  26. #define STRSIZ 256
  27.  
  28. #include <stdio.h>    /* this is your standard header for all C compiles */
  29. #include <ctype.h>
  30.  
  31. #ifdef __TURBOC__           /* v2.0b */
  32. #include <timeb.h>      /* for structure ftime */
  33. #include <io.h>         /* for setftime(), dup(), creat() */
  34. #include <mem.h>        /* for memcpy() */
  35. #include <stat.h>       /* for S_IWRITE, S_IREAD */
  36. #endif
  37.  
  38. #ifdef    AMIGA
  39. #include <stdlib.h>
  40. #include <proto/exec.h>
  41. #include <string.h>
  42. #else
  43.  
  44. #ifdef __STDC__
  45.  
  46. #include <stdlib.h>
  47.  /* this include defines various standard library prototypes */
  48.  
  49. #else
  50.  
  51. char *malloc();         /* who knows WHERE this is prototyped... */
  52.  
  53. #endif
  54.  
  55. #define min(a,b) ((a) < (b) ? (a) : (b))
  56. #endif    /* AMIGA */
  57.  
  58. #ifndef ZMEM                            /* v2.0f use your system's stuff */
  59. #define zmemcpy memcpy
  60. #define zmemset memset
  61. #endif
  62.  
  63. /* ----------------------------------------------------------- */
  64. /*
  65.  * Zipfile layout declarations
  66.  *
  67.  */
  68.  
  69. /* Macros for accessing the longint header fields.  These fields
  70.    are defined as array of char to prevent a 32-bit compiler from
  71.    padding the struct so that longints start on a 4-byte boundary.
  72.    This will not work on a machine that can access longints only
  73.    if they start on a 4-byte boundary.
  74. */
  75.  
  76. #ifndef NOTINT16    /* v2.0c */
  77. #define LONGIP(l) ((longint *) &((l)[0]))
  78. #define LONGI(l) (*(LONGIP(l)))
  79. #else       /* Have to define, since used for HIGH_LOW */
  80. #define LONGIP(I) &I
  81. #define LONGI(I) I
  82. #endif
  83.  
  84. typedef longint signature_type;
  85.  
  86. #define LOCAL_FILE_HEADER_SIGNATURE  0x04034b50L
  87.  
  88. #ifndef NOTINT16            /* v2.0c */
  89. typedef struct local_file_header {
  90.     UWORD version_needed_to_extract;
  91.     UWORD general_purpose_bit_flag;
  92.     UWORD compression_method;
  93.     UWORD last_mod_file_time;
  94.     UWORD last_mod_file_date;
  95.     byte crc32[4];
  96.     byte compressed_size[4];
  97.     byte uncompressed_size[4];
  98.     UWORD filename_length;
  99.     UWORD extra_field_length;
  100. } local_file_header;
  101.  
  102. #else   /* NOTINT16 */
  103. typedef struct local_file_header {
  104.     UWORD version_needed_to_extract;
  105.     UWORD general_purpose_bit_flag;
  106.     UWORD compression_method;
  107.     UWORD last_mod_file_time;
  108.     UWORD last_mod_file_date;
  109.     longint crc32;              /* v2.0e */
  110.     longint compressed_size;
  111.     longint uncompressed_size;  /* v2.0e */
  112.     UWORD filename_length;
  113.     UWORD extra_field_length;
  114. } local_file_header;
  115.  
  116. typedef struct local_byte_header {
  117.     byte version_needed_to_extract[2];
  118.     byte general_purpose_bit_flag[2];
  119.     byte compression_method[2];
  120.     byte last_mod_file_time[2];
  121.     byte last_mod_file_date[2];
  122.     byte crc32[4];
  123.     byte compressed_size[4];
  124.     byte uncompressed_size[4];
  125.     byte filename_length[2];
  126.     byte extra_field_length[2];
  127. } local_byte_header;
  128. #endif
  129.  
  130. #define CENTRAL_FILE_HEADER_SIGNATURE  0x02014b50L
  131.  
  132. #ifndef NOTINT16            /* v2.0c */
  133. typedef struct central_directory_file_header {
  134.     UWORD version_made_by;
  135.     UWORD version_needed_to_extract;
  136.     UWORD general_purpose_bit_flag;
  137.     UWORD compression_method;
  138.     UWORD last_mod_file_time;
  139.     UWORD last_mod_file_date;
  140.     byte crc32[4];
  141.     byte compressed_size[4];
  142.     byte uncompressed_size[4];
  143.     UWORD filename_length;
  144.     UWORD extra_field_length;
  145.     UWORD file_comment_length;
  146.     UWORD disk_number_start;
  147.     UWORD internal_file_attributes;
  148.     byte external_file_attributes[4];
  149.     byte relative_offset_local_header[4];
  150. } central_directory_file_header;
  151.  
  152. #else   /* NOTINT16 */
  153. typedef struct central_directory_file_header {
  154.     UWORD version_made_by;
  155.     UWORD version_needed_to_extract;
  156.     UWORD general_purpose_bit_flag;
  157.     UWORD compression_method;
  158.     UWORD last_mod_file_time;
  159.     UWORD last_mod_file_date;
  160.     longint crc32;                  /* v2.0e */
  161.     longint compressed_size;        /* v2.0e */
  162.     longint uncompressed_size;      /* v2.0e */
  163.     UWORD filename_length;
  164.     UWORD extra_field_length;
  165.     UWORD file_comment_length;
  166.     UWORD disk_number_start;
  167.     UWORD internal_file_attributes;
  168.     longint external_file_attributes;       /* v2.0e */
  169.     longint relative_offset_local_header;   /* v2.0e */
  170. } central_directory_file_header;
  171.  
  172. typedef struct central_directory_byte_header {
  173.     byte version_made_by[2];
  174.     byte version_needed_to_extract[2];
  175.     byte general_purpose_bit_flag[2];
  176.     byte compression_method[2];
  177.     byte last_mod_file_time[2];
  178.     byte last_mod_file_date[2];
  179.     byte crc32[4];
  180.     byte compressed_size[4];
  181.     byte uncompressed_size[4];
  182.     byte filename_length[2];
  183.     byte extra_field_length[2];
  184.     byte file_comment_length[2];
  185.     byte disk_number_start[2];
  186.     byte internal_file_attributes[2];
  187.     byte external_file_attributes[4];
  188.     byte relative_offset_local_header[4];
  189. } central_directory_byte_header;
  190. #endif
  191.  
  192. #define END_CENTRAL_DIR_SIGNATURE  0x06054b50L
  193.  
  194. #ifndef NOTINT16            /* v2.0c */
  195. typedef struct end_central_dir_record {
  196.     UWORD number_this_disk;
  197.     UWORD number_disk_with_start_central_directory;
  198.     UWORD total_entries_central_dir_on_this_disk;
  199.     UWORD total_entries_central_dir;
  200.     byte size_central_directory[4];
  201.     byte offset_start_central_directory[4];
  202.     UWORD zipfile_comment_length;
  203. } end_central_dir_record;
  204.  
  205. #else   /* NOTINT16 */
  206. typedef struct end_central_dir_record {
  207.     UWORD number_this_disk;
  208.     UWORD number_disk_with_start_central_directory;
  209.     UWORD total_entries_central_dir_on_this_disk;
  210.     UWORD total_entries_central_dir;
  211.     longint size_central_directory;         /* v2.0e */
  212.     longint offset_start_central_directory; /* v2.0e */
  213.     UWORD zipfile_comment_length;
  214. } end_central_dir_record;
  215.  
  216. typedef struct end_central_byte_record {
  217.     byte number_this_disk[2];
  218.     byte number_disk_with_start_central_directory[2];
  219.     byte total_entries_central_dir_on_this_disk[2];
  220.     byte total_entries_central_dir[2];
  221.     byte size_central_directory[4];
  222.     byte offset_start_central_directory[4];
  223.     byte zipfile_comment_length[2];
  224. } end_central_byte_record;
  225. #endif  /* NOTINT16 */
  226.  
  227. #define DLE 144
  228.  
  229. #define max_bits 13
  230. #define init_bits 9
  231. #define hsize 8192
  232. #define first_ent 257
  233. #define clear 256
  234.  
  235. /* ============================================================= */
  236. /*
  237.  * Host operating system details
  238.  *
  239.  */
  240.  
  241. #ifdef UNIX
  242.  
  243. /* On some systems the contents of sys/param.h duplicates the
  244.    contents of sys/types.h, so you don't need (and can't use)
  245.    sys/types.h. */
  246.  
  247. #include <sys/types.h>
  248. #include <sys/param.h>
  249.  
  250. #define ZSUFX ".zip"
  251. #ifndef BSIZE
  252. #define BSIZE DEV_BSIZE     /* v2.0c assume common for all Unix systems */
  253. #endif
  254.  
  255. #ifndef BSD                 /* v2.0b */
  256. #include <time.h>
  257. struct tm *gmtime(), *localtime();
  258. #else   /* BSD */
  259. #include <sys/time.h>
  260. #endif
  261.  
  262. #else   /* !UNIX */
  263.  
  264. #define BSIZE 512   /* disk block size */
  265. #define ZSUFX ".ZIP"
  266.  
  267. #endif
  268.  
  269. #if defined(V7) || defined(BSD)
  270.  
  271. #define strchr index
  272. #define strrchr rindex
  273.  
  274. #endif
  275.  
  276. /*  v3.03 Everybody seems to need this.
  277.  * this include file defines
  278.  *             #define S_IREAD 0x0100  (* owner may read *)
  279.  *             #define S_IWRITE 0x0080 (* owner may write *)
  280.  * as used in the creat() standard function
  281.  */
  282.  
  283. #ifdef    AMIGA
  284. #include <string.h>
  285.  /* this include defines strcpy, strcmp, etc. */
  286. #else
  287.  
  288. #ifndef __TURBOC__    /* it's already included */
  289. #include <sys/stat.h>    /* for S_IWRITE, S_IREAD  v3.03 */
  290. #endif
  291.  
  292. #ifdef __STDC__
  293.  
  294. #include <string.h>
  295.  /* this include defines strcpy, strcmp, etc. */
  296.  
  297. #else
  298.  
  299. char *strchr(), *strrchr();
  300.  
  301. #endif
  302. #endif    /* AMIGA */
  303.  
  304. long lseek();
  305.  
  306. #define SEEK_SET  0
  307. #define SEEK_CUR  1
  308. #define SEEK_END  2
  309.  
  310. #ifdef V7
  311.  
  312. #define O_RDONLY  0
  313. #define O_WRONLY  1
  314. #define O_RDWR    2
  315.  
  316. #else   /* !V7 */
  317.  
  318. #include <fcntl.h>
  319.  /*
  320.   * this include file defines
  321.   *             #define O_BINARY 0x8000  (* no cr-lf translation *)
  322.   * as used in the open() standard function
  323.   */
  324.  
  325. #endif  /* V7 */
  326.  
  327. #ifdef __TURBOC__
  328. /* v2.0b Local Prototypes */
  329.     /* In CRC32.C */
  330. extern void UpdateCRC(register unsigned char *s, register int len);
  331.     /* In MATCH.C */
  332. extern int match(char *string, char *pattern);
  333.     /* v2.0j in mapname.c */
  334. extern int mapped_name(void);
  335.  
  336. #ifdef NOTINT16         /* v2.0c */
  337. /* The next two are only prototyped here for debug testing on my PC
  338.  * with Turbo C.
  339.  */
  340.  
  341. UWORD makeword(byte *b);
  342. longint makelong(byte *sig);    /* v2.0e */
  343. #endif  /* NOTINT16 */
  344.  
  345. int ReadByte(UWORD *x);
  346. int FillBitBuffer(register int bits);
  347. void LoadFollowers(void);
  348. void FlushOutput(void);
  349. void partial_clear(void);
  350. int create_output_file(void);
  351. void unShrink(void);
  352. void unReduce(void);
  353. void unImplode(void);
  354. void set_file_time(void);
  355. int readbuf(int fd, char *buf, register unsigned size);
  356. void get_string(int len, char *s);
  357. void dir_member(void);
  358. void extract_member(void);
  359. void skip_member(void);
  360. void process_local_file_header(char **fnamev);
  361. void process_central_file_header(void);
  362. void process_end_central_dir(void);
  363. int open_input_file(void);
  364. void process_headers(void);
  365. void usage(void);
  366. void process_zipfile(void);
  367. #endif  /* __TURBOC__ */
  368.  
  369. /* ------------------------------------------------------------- */
  370.  
  371. /* v3.05 Deleting  crc32.h and incorporating its two lines right here */
  372. /* #include "crc32.h" */
  373.  
  374. extern unsigned long crc32val;
  375. #ifndef __TURBOC__    /* it's already been prototyped above */
  376. void UpdateCRC();
  377. #endif
  378.  
  379. #define LF 10        /* '\n' on ascii machines.  Must be 10 due to EBCDIC */
  380. #define CR 13        /* '\r' on ascii machines.  Must be 13 due to EBCDIC */
  381.  
  382. #ifdef EBCDIC
  383. extern unsigned char ebcdic [];
  384. #define ascii_to_native(c) ebcdic[(c)]
  385. #else
  386. #define ascii_to_native(c) (c)
  387. #endif
  388.  
  389. #define OUTB(intc) { *outptr++=intc; if (++outcnt==OUTBUFSIZ) FlushOutput(); }
  390.  
  391. /*
  392.  *  macro OUTB(intc)
  393.  *  {
  394.  *      *outptr++=intc;
  395.  *      if (++outcnt==OUTBUFSIZ)
  396.  *          FlushOutput();
  397.  *  }
  398.  *
  399.  */
  400.  
  401. #define READBIT(nbits,zdest) { if (nbits <= bits_left) { zdest = (int)(bitbuf & mask_bits[nbits]); bitbuf >>= nbits; bits_left -= nbits; } else zdest = FillBitBuffer(nbits);}
  402.  
  403. /*
  404.  * macro READBIT(nbits,zdest)
  405.  *  {
  406.  *      if (nbits <= bits_left) {
  407.  *          zdest = (int)(bitbuf & mask_bits[nbits]);
  408.  *          bitbuf >>= nbits;
  409.  *          bits_left -= nbits;
  410.  *      } else
  411.  *          zdest = FillBitBuffer(nbits);
  412.  *  }
  413.  *
  414.  */
  415.